#include <reg52.h>

sbit k1 = P3^4;
sbit dula = P2^6;
sbit wela = P2^7;

unsigned char i, j, temp;
unsigned char code table[] = {
			0x3f, 0x06, 0x5b, 0x4f,
			0x66, 0x6d, 0x7d, 0x07,
			0x7f, 0x6f, 0x77, 0x7c,
			0x39, 0x5e, 0x79, 0x71, 0};

void delay(unsigned int k)
{
	unsigned int i, j;
	for(j = k; j > 0; j--)
		for(i = 110; i > 0; i--);
}

unsigned char key_scan()
{
	P3 = 0xfe;
	temp = P3;
	temp = temp & 0xf0;
	while (temp != 0xf0)
	{
		delay(5);
		temp = P3;
		temp = temp & 0xf0;
		while (temp != 0xf0)
		{
			temp = P3;
			switch (temp)
			{
				case 0xee : i = 1;
					break;
				case 0xde : i = 2;
					break;
				case 0xbe : i = 3;
					break;
				case 0x7e : i = 4;
					break;
				default :
					break;
			}
			while (temp != 0xf0)
			{
				temp = P3;
				temp = temp & 0xf0;
			}
		}
	}
	
	P3 = 0xfd;
	temp = P3;
	temp = temp & 0xf0;
	while (temp != 0xf0)
	{
		delay(5);
		temp = P3;
		temp = temp & 0xf0;
		while (temp != 0xf0)
		{
			temp = P3;
			switch (temp)
			{
				case 0xed : i = 5;
					break;
				case 0xdd : i = 6;
					break;
				case 0xbd : i = 7;
					break;
				case 0x7d : i = 8;
					break;
				default :
					break;
			}
			while (temp != 0xf0)
			{
				temp = P3;
				temp = temp & 0xf0;
			}
		}
	}
	
	P3 = 0xfb;
	temp = P3;
	temp = temp & 0xf0;
	while (temp != 0xf0)
	{
		delay(5);
		temp = P3;
		temp = temp & 0xf0;
		while (temp != 0xf0)
		{
			temp = P3;
			switch (temp)
			{
				case 0xeb : i = 9;
					break;
				case 0xdb : i = 10;
					break;
				case 0xbb : i = 11;
					break;
				case 0x7b : i = 12;
					break;
				default :
					break;
			}
			while (temp != 0xf0)
			{
				temp = P3;
				temp = temp & 0xf0;
			}
		}
	}
	
	P3 = 0xf7;
	temp = P3;
	temp = temp & 0xf0;
	while (temp != 0xf0)
	{
		delay(5);
		temp = P3;
		temp = temp & 0xf0;
		while (temp != 0xf0)
		{
			temp = P3;
			switch (temp)
			{
				case 0xe7 : i = 13;
					break;
				case 0xd7 : i = 14;
					break;
				case 0xb7 : i = 15;
					break;
				case 0x77 : i = 16;
					break;
				default :
					break;
			}
			while (temp != 0xf0)
			{
				temp = P3;
				temp = temp & 0xf0;
			}
		}
	}
	return i;
}

void main()
{	
	i = 17;
	dula = 1;
	P0 = 0x00;
	dula = 0;
	
	wela = 1;
	P0 = 0xc0;
	wela = 0;
	
	while (1)
	{
		j = key_scan();
		dula = 1;
		P0 = table[j-1];
		dula = 0;
	}
}
















